package com.itheima.threadpool;

import java.util.concurrent.*;

/**
 * @author Anomaly
 * @date 2023/2/10 22:45
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 6, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        Runnable target = new MyRun();
        pool.execute(target);
        pool.execute(target);
        pool.execute(target);
        //进入任务队列
        pool.execute(target);
        pool.execute(target);
        pool.execute(target);
        pool.execute(target);
        pool.execute(target);
        //创建临时线程
        pool.execute(target);
        pool.execute(target);
        //触发拒绝策略 抛异常
        pool.execute(target);
    }
}

class MyRun implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + "输出了： " + i);
        }
        try {
            System.out.println(Thread.currentThread().getName() + "开始休眠");
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
